package com.tianxin.workbao.workBao.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.bean.TestItem;
import com.tianxin.workbao.workBao.holder.EmptyHolder;

import java.util.List;

/**
 * Created by hacker on 2017/7/17.
 */

public class TestItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_EMPTY = 0;
    private static final int TYPE_DEFAULT = 1;
    List<TestItem> mTestitems;
    private Context mContext;

    public TestItemAdapter(List<TestItem> list, Context context) {
        this.mTestitems = list;
        this.mContext = context;
    }

    @Override
    public int getItemViewType(int position) {
        if (mTestitems.size() > 0) {
            return TYPE_DEFAULT;
        } else {
            return TYPE_EMPTY;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if (viewType == TYPE_DEFAULT) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.test_item_item, parent, false);
            return new ViewHolder(view);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty, parent, false);
            return new EmptyHolder(view);
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            ViewHolder viewHolder = (ViewHolder) holder;
            viewHolder.BindViewData(mTestitems.get(position));
        }
    }

    @Override
    public int getItemCount() {
        if (mTestitems.size() > 0) {
            return mTestitems.size();
        } else {
            return 1;
        }
    }

    public int getItemColumnSpan(int position) {
        switch (getItemViewType(position)) {
            case TYPE_DEFAULT:
                return 1;
            default:
                return 0;
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView testItemNameTV;
        TextView testItemEnNameTV;
        TextView testItemKeyTV;

        public ViewHolder(View itemView) {
            super(itemView);
            testItemNameTV = (TextView) itemView.findViewById(R.id.test_item_name);
            testItemEnNameTV = (TextView) itemView.findViewById(R.id.test_item_en_name);
            testItemKeyTV = (TextView) itemView.findViewById(R.id.test_item_key);
        }

        public void BindViewData(TestItem testItem) {
            testItemNameTV.setText(testItem.getItemname());
            testItemEnNameTV.setText(testItem.getEnname());
            testItemKeyTV.setText(testItem.getItemkey());
        }
    }
}
